package com.zdsoft.crm.controller.clue;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zdsoft.crm.controller.BaseController;
import com.zdsoft.crm.entity.Clue;
import com.zdsoft.crm.entity.auth.SysUser;
import com.zdsoft.crm.service.auth.ISysUserService;
import com.zdsoft.crm.service.clue.ICluePoolService;
import com.zdsoft.crm.service.clue.IClueService;
import com.zdsoft.crm.vo.PageVo;

@Controller
@RequestMapping("/cluePool")
public class CluePoolController extends BaseController {

	@Autowired
	ICluePoolService cluePoolService;
	
	@Autowired
	IClueService clueService;

	@Autowired
	ISysUserService iSysUserService;
	
	
	@Autowired
	HttpSession session;
	
	
	@GetMapping("/list")
	public ModelAndView clue(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, 
			@RequestParam(name = "CriteriaType", defaultValue = "-1") String CriteriaType,
			@RequestParam(name = "content", defaultValue = "") String content) throws UnsupportedEncodingException {
		
		if(CriteriaType.equals("-1")){
			content="";
		}
		content = new String(content.getBytes("ISO8859-1"), "UTF-8");
		PageVo list = cluePoolService.pageListWhere(pageNum, pageSize,CriteriaType,content);

		ModelAndView mav=new ModelAndView("clue/cluePool/list.html", "list", list);
		mav.addObject("CriteriaType", CriteriaType);
		mav.addObject("content", content);
		return mav;
	}
	
	@DeleteMapping("/delete")
	@ResponseBody // ajax提交时必须需要加的注解
	public void deleteAll(@RequestBody String idsStr) {
		System.out.println(idsStr);
		String[] list=idsStr.split(",");
		if(list.length!=0){
			for (String string : list) {
				cluePoolService.delete(string);
			}
		}
	}
	
	/**
	 * 领取
	 * @param id
	 */
	@PostMapping("/getMyClue")
	@ResponseBody // ajax提交时必须需要加的注解
	public boolean getMyClue(String id) {
		Clue clue=clueService.getClueInfo(id);
		
		SysUser user=(SysUser)session.getAttribute("user");
		
		if(user!=null){
			clue.setCharge_user_id(user.getId());
			//设置session中的userId
			int row=cluePoolService.getMyClue(clue);
			if(row!=0){
				return true;
			}
			return false;
		}
		return false;
	}
	
	/**
	 * 分配
	 * @param id
	 */
	@GetMapping("/distribute")
	public ModelAndView distributeUI( String id,@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
			@RequestParam(name = "username", defaultValue = "") String username) {
		ModelAndView mav = new ModelAndView("clue/charge/user/distribute.html", "list",
				iSysUserService.findPage(pageNum, pageSize, username));
		mav.addObject("username", username);
		mav.addObject("clueId", id);
		return mav; // 省代码(视图解析器)
	}
	
	/**
	 * 分配
	 * @param id
	 */
	@PostMapping("/distribute")
	@ResponseBody
	public boolean distribute(String clueId,String userId) {
		int row=cluePoolService.distributeClue(clueId, userId);
		if(row!=0){
			return true;
		}
		return false;
	}
}
